<?php

namespace app\api\controller;

use app\api\model\System as SystemModel;
use app\BaseController;
use app\lib\tool\Map;

class System extends BaseController
{
    /* 获取 */
    public function getList()
    {
        $data = SystemModel::order('id desc')->select();
        return show(config('status.success'), '获取社区列表', $data);
    }

    /* 获取 */
    public function getOne()
    {
        $id = input('id', 0, 'intval');
        $data = SystemModel::where('id', $id)->find();
        return show(config('status.success'), '获取社区信息', $data);
    }

    /* 获取当前位置定位 */
    public function location()
    {
        $arr = SystemModel::field('id,map')->where('status', 1)->select();
        $lat = input('lat', '');
        $lng = input('lng', '');
        $minId = 0;
        $minDistance = 0;
        foreach ($arr as $v) {
            $maps = explode(',', $v['map']);
            $distance = Map::getDistance($lat, $lng, $maps[0], $maps[1]);

            if ($distance == 0) {
                $minDistance = 0;
                $minId = $v['id'];
                break;
            }

            if ($minDistance == 0) {
                $minDistance = $distance;
                $minId = $v['id'];
            } else {
                if ($distance < $minDistance) {
                    $minDistance = $distance;
                    $minId = $v['id'];
                }
            }
        }

        $data = SystemModel::where('id', $minId)->find();
        return show(config('status.success'), '获取定位成功', $data);
    }

    /* 获取推荐 */
    public function recommend()
    {
        $data = SystemModel::where('id', 1)->find();
        return show(config('status.success'), '获取推荐社区', $data);
    }
}
